<template>
  <div class="login-container">
    <div class="background"></div>
    <div class="login-form">
      <div class="title">
        <a-icon type="icon-Union4" class="icon"></a-icon>
        <span> 简历管理系统</span>
      </div>
      <div class="form-wrap">
        <a-form
          :model="loginForm"
          name="basic"
          :label-col="{ span: 5 }"
          :wrapper-col="{ span: 20 }"
          autocomplete="off"
          @submit="handleSubmit"
        >
          <a-form-item
            label="用户名"
            name="userAccount"
            :rules="[{ required: true, message: '请输入用户名!' }]"
          >
            <a-input v-model:value="loginForm.userAccount" />
          </a-form-item>
          <a-form-item
            label="密码"
            name="userPassword"
            :rules="[{ required: true, message: '请输入密码!' }]"
          >
            <a-input-password v-model:value="loginForm.userPassword" />
          </a-form-item>
        </a-form>
        <div class="button-group">
          <a-button class="login-button" type="primary" @click="handleLogin">登录</a-button>
          <a-button class="register-button" @click="handleRegister">注册</a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import server from '@/api'
import { reactive } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'
import { message } from 'ant-design-vue'

const store = useUserStore()
const router = useRouter()

const loginForm = reactive({
  userAccount: '',
  userPassword: '',
})

const handleLogin = async () => {
  try {
    const res = await server.login({
      userAccount: loginForm.userAccount,
      userPassword: loginForm.userPassword,
    })
    // 假设服务器响应成功，有正确的登录标识
    if (res.data.code === 0) {
      let temp = res.data.data.userName === null ? '' : res.data.data.userName
      store.userName = res.data.data.userRole + temp
      store.userTureName = res.data.data.userName
      store.userRole = res.data.data.userRole
      store.userID = res.data.data.id
      store.userAccount = res.data.data.userAccount
      router.push('/')
    } else {
      message.error(res.data.message)
    }
  } catch (error) {
    message.error(error)
  }
}
const handleRegister = () => {
  router.push('/register')
}
</script>

<style lang="scss" scoped>
.login-container {
  position: relative;
  width: 100%;
  height: 100vh;
}

.background {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-position: center;
  background-size: cover;
  background-color: #011427;
  background-image: url('@/assets/images/login-bg.jpg');
}

.login-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 25%;
  right: 20%;
  width: 350px;
  min-width: 300px;
  height: 400px;
  border-radius: 8px;
  background-color: #e8e8e8;
  box-shadow: 0 0 10px rgb(0 0 0 / 10%);
}

.form-wrap {
  width: 90%;
  padding: 10px;

  .button-group {
    margin-left: 65px;

    .login-button {
      width: 100px;
    }

    .register-button {
      width: 100px;
      margin-left: 50px;
    }
  }
}

.title {
  height: 50px;
  font-size: 16px;
  text-align: center;
  line-height: 50px;

  .icon {
    margin-right: 10px;
    font-size: 16px;
  }
}

a-form-item {
  width: 100%;
  margin-bottom: 20px;
}
</style>
